package org.hong.monkey.launcher

import java.io.File
import java.util.{HashMap => JHashMap, List => JList, Map => JMap}

import scala.collection.JavaConverters._

import org.hong.monkey.deploy.Command

/**
 * This class is used by CommandUtils. It uses some package-private APIs in MonkeyLauncher, and since
 * Java doesn't have a feature similar to `private[monkey]`, and we don't want that class to be
 * public, needs to live in the same package as the rest of the library.
 */
private[monkey] class WorkerCommandBuilder(monkeyHome: String, memoryMb: Int, command: Command)
    extends AbstractCommandBuilder {

  childEnv.putAll(command.environment.asJava)
  childEnv.put(CommandBuilderUtils.ENV_MONKEY_HOME, monkeyHome)

  override def buildCommand(env: JMap[String, String]): JList[String] = {
    val cmd = buildJavaCommand(command.classPathEntries.mkString(File.pathSeparator))
    cmd.add(s"-Xms${memoryMb}M")
    cmd.add(s"-Xmx${memoryMb}M")
    command.javaOpts.foreach(cmd.add)
    CommandBuilderUtils.addPermGenSizeOpt(cmd)
    addOptionString(cmd, getenv("MONKEY_JAVA_OPTS"))
    cmd
  }

  def buildCommand(): JList[String] = buildCommand(new JHashMap[String, String]())

}
